package com.siwei.apply.controller;

import com.siwei.apply.service.NodeLandService;
import com.siwei.common.core.domain.R;
import com.siwei.common.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 节点地块关联控制器
 */
@RestController
@RequestMapping("/nodeland")
public class NodeLandController extends BaseController {

    @Autowired
    private NodeLandService nodeLandService;

    /**
     * 根据nodeId和geomDbId创建节点地块关联记录
     *
     * @param requestBody 包含nodeId和geomDbId的请求体
     * @return 创建结果
     */
    @PostMapping("/create")
    public R<Map<String, Object>> createNodeLand(@RequestBody Map<String, String> requestBody) {
        try {
            String nodeId = requestBody.get("nodeId");
            String geomDbId = requestBody.get("geomDbId");
            if (nodeId == null || nodeId.trim().isEmpty()) {
                return R.fail("nodeId不能为空");
            }
            if (geomDbId == null || geomDbId.trim().isEmpty()) {
                return R.fail("geomDbId不能为空");
            }
            nodeLandService.createNodeLand(nodeId, geomDbId);

            return R.ok();
        } catch (Exception e) {
            logger.error("创建节点地块关联记录异常", e);
            return R.fail("创建节点地块关联记录异常：" + e.getMessage());
        }
    }

    /**
     * 根据nodeId查询地块几何信息
     *
     * @param nodeId 节点ID
     * @return 地块几何信息，包含geom_db_id和并集的外边框、中心点
     */
    @GetMapping("/geom/{nodeId}")
    public R<Map<String, Object>> getGeomByNodeId(@PathVariable String nodeId) {
        try {
            if (nodeId == null || nodeId.trim().isEmpty()) {
                return R.fail("nodeId不能为空");
            }

            Map<String, Object> geomInfo = nodeLandService.getGeomByNodeId(nodeId);
            return R.ok(geomInfo);

        } catch (Exception e) {
            logger.error("查询节点地块几何信息异常", e);
            return R.fail("查询节点地块几何信息异常：" + e.getMessage());
        }
    }

    /**
     * 根据项目ID与节点表名获取地块几何信息
     * 先查项目流程得到nodeId，再查几何信息
     */
    @GetMapping("/node/geom")
    public R<Map<String, Object>> getGeomByProjectAndNodeTable(@RequestParam String projectId,
                                                               @RequestParam String nodeTableName) {
        try {
            if (projectId == null || projectId.trim().isEmpty()) {
                return R.fail("projectId不能为空");
            }
            if (nodeTableName == null || nodeTableName.trim().isEmpty()) {
                return R.fail("nodeTableName不能为空");
            }
            Map<String, Object> geomInfo = nodeLandService.getGeomByProjectAndNodeTable(projectId, nodeTableName);
            return R.ok(geomInfo);
        } catch (Exception e) {
            logger.error("根据项目与节点表名查询地块几何信息异常", e);
            return R.fail("查询异常：" + e.getMessage());
        }
    }

    /**
     * 根据nodeId删除节点地块关联记录
     *
     * @param nodeId 节点ID
     * @return 删除结果
     */
    @DeleteMapping("/{nodeId}")
    public R<Map<String, Object>> deleteByNodeId(@PathVariable String nodeId) {
        try {
            if (nodeId == null || nodeId.trim().isEmpty()) {
                return R.fail("nodeId不能为空");
            }

            nodeLandService.deleteByNodeId(nodeId);
            return R.ok();
        } catch (Exception e) {
            logger.error("删除节点地块关联记录异常", e);
            return R.fail("删除节点地块关联记录异常：" + e.getMessage());
        }
    }
}
